package com.lx.mailweb.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;



import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.lx.mailweb.domain.Txl;
import com.lx.mailweb.domain.User;
import com.lx.mailweb.domain.Adress;
import com.lx.mailweb.persistence.mapper.BaseDaoMapper;

@Controller
public class IndexController {
	@Autowired
	private BaseDaoMapper baseDaoMapper;

	@RequestMapping(value = { "/", "/index" })
	public String index(HttpServletRequest request, Model model) {
		return "txl";
	}
	
	@RequestMapping("/adress")
	public String adressGrid(HttpServletRequest request, Model model) {

		List<Adress> AdressList = baseDaoMapper.getAdress();
		model.addAttribute("adressList", AdressList);
		return "adress";
	}
	
	@RequestMapping("/adress_edit")
	public String adressGridedit(HttpServletRequest request, Model model) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
		List<Adress> AdressList = baseDaoMapper.getAdress();
		model.addAttribute("adressList", AdressList);
		return "admin/adress_edit";
	}    
		 return "admin/login";
			}
	
	@RequestMapping("/txlgrid")
	public String txlGrid(HttpServletRequest request, Model model,String bumen ,String name) {

		List<Txl> txlList = baseDaoMapper.getTxl(bumen,name);
		model.addAttribute("txlList", txlList);
		return "txlGrid";
	}
	@RequestMapping("/txlgrid2")
	public String txlGrid2(HttpServletRequest request, Model model,String bumen ,String name) {

		List<Txl> txlList = baseDaoMapper.getTxl(bumen,name);
		model.addAttribute("txlList", txlList);
		return "admin/txlGrid2";
	}
	
	@RequestMapping("/zhiwei_edit")
	public String mian(HttpServletRequest request) {
	  User user = (User) request.getSession().getAttribute("admin");
	  if (user != null) {
		return "/admin/zhiwei_edit";
	  }
	  return "admin/login";
	}

	@RequestMapping("/txl_edit")
	
	public String edit(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
		return "/admin/txl_edit";
	}return "admin/login";
	}
	
	@RequestMapping("/zhiweigrid")
	public String zhiweiGrid(HttpServletRequest request, Model model,String bumen ,String name) {

		List<Txl> txlList = baseDaoMapper.getTxl(bumen,name);
		model.addAttribute("txlList", txlList);
		return "/admin/zhiweiGrid";
	}
	
	@RequestMapping(value = "/updateTxl", method = RequestMethod.POST)
	public @ResponseBody
	String updateTxl(Txl txl) {
		if (baseDaoMapper.updateTxl(txl)>0)
			return new String("success");
		return new String("fail");
	}
	
	@RequestMapping(value = "/saveTxl", method = RequestMethod.POST)
	public @ResponseBody
	String saveTxl(Txl txl) {
		if (baseDaoMapper.saveTxl(txl)>0)
			return new String("success");
		return new String("fail");
	}
	
	
	@RequestMapping(value = "/saveAdress", method = RequestMethod.POST)
	public @ResponseBody
	String saveAdress(Adress adress) {
		if (baseDaoMapper.saveAdress(adress)>0)
			return new String("success");
		return new String("fail");
	}
	
	
	
	
	
	@RequestMapping(value = "/delTxl", method = RequestMethod.POST)
	public @ResponseBody
	String delTxl(Txl txl) {
		if (baseDaoMapper.delTxl(txl)>0)
			return new String("success");
		return new String("fail");
	}
	
	@RequestMapping("/txl_revise")
	public String getall(HttpServletRequest request, Model model,String id1) {

		Txl txl = baseDaoMapper.getOne(id1);
		//one record
		model.addAttribute("txl", txl);
		return "admin/txl_revise" ;
	}
	

	@RequestMapping("/txl_add")
	public String add(HttpServletRequest request, Model model,int ord) {
		model.addAttribute("ord", ord);
		return "admin/txl_add" ;
	}
	
	
	
	
	@RequestMapping(value = "/addTxl", method = RequestMethod.POST)
	public @ResponseBody
	    String addTxl (Txl txl){
		//��update order��add Txl
		if (baseDaoMapper.addord(txl)>0){
			int i=baseDaoMapper.addTxl(txl);
			if(i>0){
				return new String("success");
			}
		}
		return new String("fail");
	}
	
	@RequestMapping("/adress_revise")
	public String getadress(HttpServletRequest request, Model model,String id) {

		Adress adress = baseDaoMapper.getAdressone(id);
		//one record
		model.addAttribute("adress", adress);
		return "admin/adress_revise" ;
	}
	
	
	
/*	@RequestMapping("/main")
	public String miana(HttpServletRequest request) {
		return "/admin/main";
	}
	
	*/
	
	
	/*@RequestMapping("/showgrid")
	public String showGrid(HttpServletRequest request, Model model,int zjl ,int bm,int pq,int fzr,int zx,int zhnq,String[]bumen){

		List<Txl> txlList = baseDaoMapper.getSr(zjl,bm,pq,fzr,zx,zhnq,bumen);
		model.addAttribute("txlList", txlList);
		return "showgrid";*/
	
	@RequestMapping("/showgrid")
	public String showGrid(HttpServletRequest request, Model model,String zhiwei,String[]bumen){
		List<Txl> txlList = baseDaoMapper.getEmail(zhiwei,bumen);
		List<Txl> txlList3 = baseDaoMapper.getEmail1(zhiwei,bumen);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("zhiwei", zhiwei);
		Set<Txl> txlList1 = baseDaoMapper.getEmail2(map);
		/*for (Txl txl : txlList1) {
			System.out.println(txl.getEmail());
		}
		System.out.println("-----------------");*/
		map=new HashMap<String, Object>();
		map.put("bumen", bumen);
		Set<Txl> txlList5 = baseDaoMapper.getEmail2(map);
		/*for (Txl txl : txlList5) {
			System.err.println(txl.getEmail());
		}
		System.out.println("-----------------");*/
		txlList5.addAll(txlList1);
		model.addAttribute("txlList", txlList);
		model.addAttribute("txlList5", txlList5);
		/*System.out.println("-----------------");
		for (Txl txl : txlList5) {
			System.out.println(txl.getEmail());
		}*/
		model.addAttribute("txlList3", txlList3);
		return "showGrid";
	}
	
	

	
}



